package br.com.comanda.webservice.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import br.com.comanda.webservice.dao.GenericDAO;

public class GenericDAOImpl<T> implements GenericDAO<T> {

	private Session session;
	private final Class class1;

	public GenericDAOImpl(Class<T> class1) {
		this.class1 = class1;
	}

	public void save(T o) {
		this.session.beginTransaction().begin();
		this.session.save(o);
		this.session.beginTransaction().commit();
	}

	public void delete(T o) {
		this.session.beginTransaction().begin();
		this.session.delete(o);
		this.session.beginTransaction().commit();

	}

	public T findById(Long id) {
		Criteria criteria = this.session.createCriteria(this.class1);
		criteria.add(Restrictions.eq("id", id));
		return (T) criteria.uniqueResult();
	}

	public List<T> findAll() {
		Criteria criteria = this.session.createCriteria(this.class1);
		return criteria.list();
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public Session getSession() {
		return this.session;
	}

}
